package com.ibs.hadoop.hbase.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.ibs.hadoop.hbase.bean.HbaseDataBean;
import com.ibs.hadoop.hbase.bean.QueryBean;
import com.ibs.hadoop.hbase.bean.TauBean;
import com.ibs.hadoop.hbase.bean.UseComplaintBaseClass;
import com.ibs.hadoop.hbase.console.PropertiesEnumKey;
import com.ibs.hadoop.hbase.dao.base.HbaseProceDaoBase;
import com.ibs.hadoop.hbase.util.PropertiesUtils;

public class TauDaoImpl extends HbaseProceDaoBase {

    /**
     * 表名
    * @字段说明 PREFIX_TABLE_NAME
    */
    private static final String PREFIX_TABLE_NAME = "TAU_";

    /**
     * 日志 
    * @字段说明 log
    */
    private Logger log = Logger.getLogger(TauDaoImpl.class);

    /**
     * 实例对象
    * @字段说明 INSTANCE
    */
    private static final TauDaoImpl INSTANCE = new TauDaoImpl();

    public static TauDaoImpl getInstance() {
        return INSTANCE;
    }

    public List<UseComplaintBaseClass> query(QueryBean query) {

        log.info("TauDaoImpl query param:" + query);

        String tableName = this.getTableName(query.getTableTime());

        List<UseComplaintBaseClass> result = null;

        List<HbaseDataBean> list = null;
        try {
            list = this.queryDef(tableName, query.getStartRowKey(), query.getEndRowKey());

            if (null != list && !list.isEmpty()) {
                result = new ArrayList<UseComplaintBaseClass>();

                for (HbaseDataBean hbaseDataBean : list) {
                    TauBean tau = this.parseBean(hbaseDataBean);

                    if (null != tau) {
                        result.add(tau);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("TauDaoImpl query param:" + query, e);
        }

        if (null != result && !result.isEmpty()) {
            log.info("TauDaoImpl query param:" + query + ",result size :" + result.size());
        } else {
            log.info("TauDaoImpl query param:" + query + ",result size  is null ! ");
        }

        return result;
    }

    /**
     * 将hbase中的列数据转换为javabean信息
    * 方法描述
    * @param hbaseLine
    * @return
    * @创建日期 2016年10月31日
    */
    private TauBean parseBean(HbaseDataBean hbaseLine) {
        TauBean bean = null;
        // 获取分隔符
        String spitFlag = PropertiesUtils.getInstance()
                .getValue(PropertiesEnumKey.HBASE_TABLE_COLUMN_SPIT_FLAG.getKey());

        String[] array = hbaseLine.getValue().split(spitFlag,-1);

        if (null != array && array.length == 45) {
            bean = new TauBean();
            
            bean.setFrame(array[0]);
            bean.setTtime(array[1]);
            bean.setLastFrame(array[2]);
            bean.setEnbId(array[3]);
            bean.setCi(array[4]);
            bean.setImsi(array[5]);
            bean.setImei(array[6]);
            bean.setMTmsi(array[7]);
            bean.setMmecode(array[8]);
            bean.setNewMmecode(array[9]);
            bean.setNewMTmsi(array[10]);
            bean.setSuccess(array[11]);
            bean.setFailureCause(array[12]);
            bean.setResponseTime(array[13]);
            bean.setFailureMsg(array[14]);
            bean.setRecordFileIndex(array[15]);
            bean.setLastFileindex(array[16]);
            bean.setEnbIp(array[17]);
            bean.setMmeIp(array[18]);
            bean.setTauType(array[19]);
            bean.setEnb(array[20]);
            bean.setMme(array[21]);
            bean.setAuthreqDelay(array[22]);
            bean.setAuthresDelay(array[23]);
            bean.setTauCmp(array[24]);
            bean.setDelayCn(array[25]);
            bean.setDelayRadio(array[26]);
            bean.setLasttime(array[27]);
            bean.setTac(array[28]);
            bean.setOldTac(array[29]);
            bean.setLac(array[30]);
            bean.setOldLac(array[31]);
            bean.setEstablishCause(array[32]);
            bean.setMmegroupId(array[33]);
            bean.setEnbUeId(array[34]);
            bean.setPsStatus(array[35]);
            bean.setTmsiCs(array[36]);
            bean.setIbsId(array[37]);
            bean.setGutiType(array[38]);
            bean.setUlTeid(array[39]);
            bean.setSGwIpS1(array[40]);
            bean.setBetweenMme(array[41]);
            bean.setZoneId(array[42]);
            bean.setOriginMcc(array[43]);
            bean.setRatType(array[44]);

        }
        return bean;
    }

    /**
     * 获取
    * 方法描述
    * @param time
    * @return
    * @创建日期 2016年10月31日
    */
    private String getTableName(String time) {
        String prefix = PropertiesUtils.getInstance().getValue(PropertiesEnumKey.HBASE_TABLE_PREFIX.getKey());
        return prefix + PREFIX_TABLE_NAME + time;
    }
    
    public static void main(String[] args) {
        TauDaoImpl dao = new TauDaoImpl();

        QueryBean bean = new QueryBean();
        bean.setImsi("999999999999999");
        bean.setStartTime("20161101");
        bean.setEndTime("20161101");

        List<UseComplaintBaseClass> list = dao.query(bean);

        System.out.println(list.size());
    }
}
